package com.pg.controller.cj;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pg.entity.EipTemp;
import com.pg.entity.cj.Wd;
import com.pg.service.EipTempService;
import com.pg.service.WdService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 温度前端控制器
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@RestController
@RequestMapping("/wd")
public class WdController extends JeecgController<Wd, WdService> {

    @Autowired
    private WdService wdService;

    @Autowired
    private EipTempService eipTempService;

    /**
     * 添加 温度
     *
     * @param wd 温度
     * @return 是否成功
     */
    @ApiOperation("添加 温度")
    @AutoLog(value = "添加 温度")
    @RequestMapping("/add")
    public Result<?> add(@RequestBody Wd wd) {
        wd.setPuid(UUID.randomUUID().toString());
        wdService.save(wd);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑 温度
     *
     * @param wd 温度
     * @return 是否成功
     */
    @ApiOperation("编辑 温度")
    @AutoLog(value = "编辑 温度")
    @RequestMapping("/edit")
    public Result<?> edit(@RequestBody Wd wd) {
        EipTemp eipTemp = new EipTemp();
        eipTemp.setPuid(UUID.randomUUID().toString());
        eipTemp.setDataType("8");
        eipTemp.setLinkPUID(wd.getPuid());
        eipTemp.setDetailedInformation(JSONObject.toJSONString(wd));
        eipTempService.save(eipTemp);
        return Result.ok("修改成功！");
    }

    /**
     * 通过id删除 温度
     *
     * @param id 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id删除 温度")
    @AutoLog(value = "通过id删除 温度")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        wdService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除 温度
     *
     * @param ids 全库唯一码集合
     * @return 是否成功
     */
    @ApiOperation("批量删除 温度")
    @AutoLog(value = "批量删除 温度")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wdService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 分页列表查询 温度
     *
     * @param wd             温度
     * @param startCheckTime 起始时间
     * @param endCheckTime   结束时间
     * @param pageNo         起始页
     * @param pageSize       每页页数
     * @param req            参数
     * @return 分页数据
     */
    @ApiOperation("分页列表查询 温度")
    @AutoLog(value = "QueryExport")
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public Result<?> selectForList(
            Wd wd,
            @RequestParam(name = "startCheckTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startCheckTime,
            @RequestParam(name = "endCheckTime", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endCheckTime,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<Wd> page = new Page<>(pageNo, pageSize);
        page.setRecords(wdService.selectForList(page, wd, startCheckTime, endCheckTime));
        return Result.ok(page);
    }

    /**
     * 通过excel导入数据 温度
     *
     * @param request  参数
     * @param response 响应
     * @return 是否成功
     */
    @ApiOperation("通过excel导入数据 温度")
    @AutoLog(value = "通过excel导入数据 温度")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Wd.class);
    }

    /**
     * 导出excel 温度
     *
     * @param request 参数
     * @param wd      温度
     */
    @ApiOperation("导出excel 温度")
    @AutoLog(value = "QueryExport")
    @RequestMapping(method = RequestMethod.GET,value = "/exportXls")
    public ModelAndView exportXls(Wd wd, HttpServletRequest request) {
        // Step.1 组装查询条件
        QueryWrapper<Wd> queryWrapper = QueryGenerator.initQueryWrapper(wd, request.getParameterMap());
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //update-begin--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据--------------------
        String selections = request.getParameter("selections");
        if (!oConvertUtils.isEmpty(selections)) {
            queryWrapper.in("puid", (Object) selections.split(","));
        }
        //update-end--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据----------------------
        List<Wd> pageList = wdService.selectForList(null, wd, null, null);

        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "内装间温度列表");
        mv.addObject(NormalExcelConstants.CLASS, Wd.class);
//        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("内装间温度列表数据", "导出人:" + user.getRealname(), "导出信息"));
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("内装间温度列表数据", "导出人:" + request.getSession().getAttribute("userName"), "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }
}

